﻿using System;
using System.Diagnostics;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
using NUnit.Framework;
using LightWeb.DomainModel;
using System.Collections.Generic;

namespace LightWeb.Infrastructure.Repositories.Tests
{
    [TestFixture]
    public class CreateDbSchemaFixture
    {
        private Configuration _cfg;

        [SetUp]
        public void SetContext()
        {
            HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize();
            _cfg = new Configuration();
            _cfg.Configure();
        }
        [Test]
        public void CreateTableSchema()
        {
            DataContext.ReCreateDatabase(); 
            DataContext.ForceInit();
        }

        [Test]
        public void DropTest()
        {
            var export = new SchemaExport(_cfg);
            export.Drop(true, true);
        }


    }

    public class SchemaUpdateFixture
    {
        private Configuration _cfg;

        [SetUp]
        public void SetContext()
        {
            //HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize();
            _cfg = new Configuration();
            _cfg.Configure();

        }

        [Test]
        public void UpdateExistingDatabaseSchemaTest()
        {

            var update = new SchemaUpdate(_cfg);
            update.Execute(true, true);

        }

        [Test]
        public void ModelValidator()
        {
            var val = new SchemaValidator(_cfg);
            val.Validate();

        }
    }

    public class DbConnectionFixture
    {
        public void Test()
        {
            //HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize();
            DataContext.Init();


            Stopwatch sw = new Stopwatch();
            sw.Start();
            try
            {
                using (OurTransactionScope ts = new OurTransactionScope())
                {
                    var context = DataContext.Current;
                    User user = new User() { LoginName = "admin", Password = "admin" };
                    context.Persist(user);

                    List<Role> RoleList = new List<Role>();
                    RoleList.Add(new Role { Name = "administrators" });
                    RoleList.Add(new Role { Name = "Anonymous" });
                    RoleList.Add(new Role { Name = "Editors" });
                    foreach (var item in RoleList)
                    {
                        context.Persist(item);
                    }

                    user = context.Read<User, int>(1);

                    user.Roles.Add(new UserRole { UserID = user, RoleID = new Role { PrimaryKey = 1 } });
                    user.Roles.Add(new UserRole { UserID = user, RoleID = new Role { PrimaryKey = 2 } });
                    user.Roles.Add(new UserRole { UserID = user, RoleID = new Role { PrimaryKey = 3 } });
                    context.Update(user);
                    ts.Complete();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            sw.Stop();
            Console.WriteLine(sw.ElapsedMilliseconds);
        }

        public void WebCoreTest()
        {
            WebCore.Security.OrmRoleProvider rp = new WebCore.Security.OrmRoleProvider();
            foreach (var item in rp.GetRolesForUser("admin"))
            {
                Console.WriteLine(item);
            }
        }
    }
}
